package com.linkage.jz.bankruptcy.fragment.Project;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Project.EditManagerUserInfoActivity;
import com.linkage.jz.bankruptcy.bean.ManagerBaseUserInfo;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.utils.SPUtils;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @author FT
 * @time 2016-07-18 16:49
 */

// JUMP 
public class ZQUserOperateDetailFragment extends Fragment {
    RecyclerView mRecyclerView;
    private Context context;
    private int mPage;
    public static final String MERCHANT_DETAILS_PAGE = "MERCHANT_DETAILS_PAGE";
    private DemoAdapter mAdapter;

    List<String> mFoodData;
    List<String> mMovieData;
    List<String> mHaveFunData;
    List<String> mEvaluationData;
    private static ManagerUserInfo mInfo;

    public static ZQUserOperateDetailFragment newInstance(int page, ManagerUserInfo info) {
        Bundle args = new Bundle();
        args.putInt(MERCHANT_DETAILS_PAGE, page);
        mInfo = info;
        ZQUserOperateDetailFragment tripFragment = new ZQUserOperateDetailFragment();
        tripFragment.setArguments(args);
        return tripFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(MERCHANT_DETAILS_PAGE);
        context = getActivity().getApplicationContext();
        setData();
        getData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pro_detailpro_about, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.mRecyclerView);
        switch (mPage){
            case 0:
                initAdapter(mFoodData);
                break;
            case 1:
                initAdapter(mMovieData);
                break;
            case 2:
                initAdapter(mHaveFunData);
                break;
            case 3:
                initAdapter(mEvaluationData);
                break;
        }
        return view;
    }

    private void setData(){
        mFoodData = new ArrayList<>();
        for(int i=0; i<1;i++){
            mFoodData.add("美食"+i);
        }
        mMovieData = new ArrayList<>();
        for(int i=0; i<1;i++){
            mMovieData.add("电影"+i);
        }
        mHaveFunData = new ArrayList<>();
        for(int i=0; i<1;i++){
            mHaveFunData.add("玩乐"+i);
        }
        mEvaluationData = new ArrayList<>();
        for(int i=0; i<1;i++){
            mEvaluationData.add("评价"+i);
        }
    }
    /**
     * 设置RecyclerView属性
     */
    private void initAdapter(List<String> data) {
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new DemoAdapter(getActivity(), R.layout.fragment_manager_detail_about_item, data);
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);//设置adapter
        //设置item点击事件
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()) {

                    case R.id.rl_edituser:
                        EditManagerUserInfoActivity.start(getActivity(),mInfo);

                        break;

                }
            }
        });

    }
    /**
     * adapter
     */
    class DemoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        int mLayoutId;
        public DemoAdapter(Context context, int LayoutId, List<String> str) {
            super( LayoutId, str);
            mLayoutId = LayoutId;
        }

        @Override
        public void convert(BaseViewHolder helper, String str) {

            helper.addOnClickListener(R.id.rl_edituser);
            helper.setText(R.id.tv_detail_name,mInfo.getUser().getUsername());
            helper.setText(R.id.tv_phone,mInfo.getUser().getMobile());
            helper.setText(R.id.tv_email,mInfo.getUser().getEmail());
            helper.setText(R.id.tv_addr,mInfo.getUser().getUsername());

        }
    }


    public void getData()
    {
        if(BankruptcyApp.tokeninfo == null|| BankruptcyApp.tokeninfo.getToken() == null)
        {
            return;
        }
//        shapeLoadingDialog.setLoadingText("加载中...");
//        shapeLoadingDialog.setCanceledOnTouchOutside(false);
//        shapeLoadingDialog.show();
        OkHttpClient okHttpClient = new OkHttpClient();
//        HashMap<String, String> params = new HashMap<String, String>();
        Random random = new Random();
        String rom = random.nextLong()+"";
        String timestamp =System.currentTimeMillis()+"";
        String userToken = SPUtils.getInstance(Consts.SPNAME).getString("userToken","");
        String userId = SPUtils.getInstance(Consts.SPNAME).getString("userId","");


        RequestBody requestBody = new FormBody.Builder()
                .add("appid","1000000001")
                .add("nonce",rom)
                .add("timestamp", timestamp)
                .add("managersTeamId",mInfo.getId())
                .add("token", BankruptcyApp.tokeninfo.getToken())
                .add("userToken", userToken)
                .build();

        String url = Consts.WEBURL_MANAGERDETAIL;
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

//                shapeLoadingDialog.dismiss();


                String str = response.body().string();

//                if(Consts.ISDEBUG)s
                try{


                    JSONObject object = new JSONObject(str);
                    if(object.optInt("code") == 0)
                    {
//                        JSONObject dataobj = object.getJSONObject("data");
//                        JSONObject groupListobj = dataobj.getJSONObject("groupList");
//                        Iterator<String> it = groupListobj.keys();
//                        List<ManagerUserInfo> list = new ArrayList<>();
                            Gson gson = new Gson();
                        ManagerBaseUserInfo info = gson.fromJson(object.optString("data"),ManagerBaseUserInfo.class);

                        str = "";

                    }

                }catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }


}
